package com.fire.rabbitmq.web;

import com.fire.rabbitmq.queue.QueueRouter;
import com.fire.rabbitmq.queue.QueueTopic;
import com.fire.rabbitmq.queue.TtlQueue;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * @author ：Mr.Fire
 * @date ：Created in 2021/4/25 16:11
 * @description：
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping("/")
public class RabbitRestController {

    @Autowired
    @Qualifier("fireRabbitTemplate")
    private RabbitTemplate rabbitTemplate;

    private final Logger log =LoggerFactory.getLogger(getClass());

    @RequestMapping("/send")
    @ResponseBody
    public String send() {
        String context = "hello==========" + new Date();
        log.info("发送消息 : " + context);
        //生产者，正在往hello这个路由规则中发送，由于没有交换机，所以路由规则就是队列名称
        this.rabbitTemplate.convertAndSend("hello", context);
        return "success";
    }

    @RequestMapping("/sendExchange")
    @ResponseBody
    public String sendToExchange(){
        String context = "exchange=======" + new Date();
        log.info("发送消息 : " + context);
        //生产者，正在往交换机发送消息，交换机会根据绑定的队列来发送，多个客户端监听同一个队列，只有一个能收到消息
        this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
        return "success";
    }

    @RequestMapping("/sendRouter")
    @ResponseBody
    public String sendToExchangeByRouter(){
        String context = "exchange=======" + new Date();
        log.info("发送路由消息 : " + context);
        //生产者，正在往交换机发送消息，队列绑定了不同路由规则，交换机会使用nr.a.key作为路由规则来发送
        this.rabbitTemplate.convertAndSend(QueueRouter.DIRECT_EXCHANGE,"nr.a.key", context);
        return "success";
    }

    @RequestMapping("/sendTopic")
    @ResponseBody
    public String sendToExchangeByTopic(){
        String context = "topic=======" + new Date();
        log.info("发送topic消息 : " + context);
        //生产者，正在往topic交换机发送消息，队列绑定了不同路由规则，交换机会使用nr.topic.b作为路由规则来发送
        // 用nr.topic.b和nr.topic.#作为路由key的队列都能收到消息
        this.rabbitTemplate.convertAndSend(QueueTopic.TOPIC_EXCHANGE,"nr.topic.b", context);
        return "success";
    }

    @RequestMapping("/sendTtl")
    @ResponseBody
    public String sendTtlExchange(){
        String context = "ttl=======" + new Date();
        log.info("发送ttl消息 : " + context);
        //生产者，正在往交换机发送延时消息
        this.rabbitTemplate.convertAndSend(TtlQueue.DELAY_EXCHANEG,"nr.ttl", context);
        return "success";
    }

    @RequestMapping("/sendAck")
    @ResponseBody
    public String sendAck() {
        String context = "exchange=======" + new Date();
        log.info("发送确认消息 : " + context);
        //生产者，正在往交换机发送消息，队列绑定了不同路由规则，交换机会使用nr.a.key作为路由规则来发送
        this.rabbitTemplate.convertAndSend(QueueRouter.DIRECT_EXCHANGE,"a.key", context);
        return "success";
    }

}
